﻿Shader "Custom/rim_1" {
	Properties {
		_Color("Color",Color) = (1.0,1.0,1.0,1.0)
		_Shininess("Shininess",Float) = 10.0
		_RimColor("RimColor",Color) = (1.0,1.0,1.0,1.0)
		//_RimPower("RimPower",Range(0.1,10.0)) = 3.0
		_RimPower("RimPower",Float) = 3.0
	}
	SubShader {
		Pass {
			Tags { "LightMode" = "ForwardBase" }

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			uniform float4 _Color;
			uniform float _Shininess;
			uniform float4 _RimColor;
			uniform float _RimPower;
			
			//unity define variables;
			uniform float4 _LightColor0;

			struct vetexInput {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct vetexOutput {
				float4 pos : SV_POSITION;
				float4 posWorld : TEXCOORD0;
				float3 normalDirection : TEXCOORD1;
			};
						
			vetexOutput vert(vetexInput v)
			{
				vetexOutput o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.posWorld=  mul(_Object2World, v.vertex);
				o.normalDirection = normalize( mul( float4( v.normal,0.0) , _World2Object).xyz); 
				return o;
			}

			float4 frag(vetexOutput o) : Color
			{
				float3 viewDirection =  normalize( _WorldSpaceCameraPos - o.posWorld.xyz);
				float3 lightDirection  = normalize( _WorldSpaceLightPos0.xyz); 
				float atten = 1.0;
				
				float3 diffuseReflection
					    = atten
					    	 * _LightColor0.xyz 
					    	 	* max( 0.0, dot( o.normalDirection, lightDirection));
				float3 specularReflection 
						= atten 
							* _LightColor0.xyz 
							 	*  max( 0.0, dot( o.normalDirection, lightDirection))
								 	* pow(
								 		 max( 0, dot( reflect( -lightDirection,o.normalDirection), viewDirection)),
								 		 	_Shininess) ;
				float rim = 1 - saturate(dot(viewDirection, o.normalDirection));
				float3 rimLighting
						 = atten
						 	* _LightColor0.xyz 
								*  _RimColor.xyz 
									* saturate( dot( o.normalDirection, lightDirection))
										* pow( rim, _RimPower);
				float3 lightFinal = diffuseReflection + specularReflection + rimLighting +  UNITY_LIGHTMODEL_AMBIENT;	
				return float4(float3(pow( rim, _RimPower)) ,1.0);
				//return float4(lightFinal * _Color.rgb  ,1.);
			}
			ENDCG
		}
	} 
	//FallBack "Diffuse"
}
